package com.gaofans.nrpc.client.route;

import java.util.List;
import java.util.Random;

/**
 * 负载均衡
 * @author GaoFans
 */
public interface LoadBalance {

    /**
     * 获取一个实例
     * @param serviceId
     * @param instances
     * @return
     */
    ServiceInstance select(String serviceId,List<ServiceInstance> instances);

    /**
     * 获取默认实例
     * @return
     */
    static LoadBalance getInstance(){
        return new RandomLoadBalance();
    }

    class RandomLoadBalance implements LoadBalance{

        private final Random random = new Random();

        @Override
        public ServiceInstance select(String serviceId, List<ServiceInstance> instances) {
            return instances.get(random.nextInt(instances.size()));
        }
    }
}
